package com.hghivln.erpwarehouse.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hghivln.erpwarehouse.system.common.lang.Constant;
import com.hghivln.erpwarehouse.system.common.lang.DataGridlView;
import com.hghivln.erpwarehouse.system.entity.Menu;
import com.hghivln.erpwarehouse.system.mapper.MenuMapper;
import com.hghivln.erpwarehouse.system.mapper.RoleMapper;
import com.hghivln.erpwarehouse.system.mapper.UserMapper;
import com.hghivln.erpwarehouse.system.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hghivln
 * @since 2021-06-19
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 查询菜单和权限
     */
    @Override
    public List<Menu> queryAllMenuForList() {
        QueryWrapper<Menu> qw = new QueryWrapper<>();
        qw.eq("type", Constant.TYPE_TOP_MENU).or().eq("type", Constant.TYPE_LEFT_MENU);
        qw.orderByAsc("ordernum");
        return getBaseMapper().selectList(qw);
    }

    @Override
    public List<Menu> queryAllMenuById(Integer id) {
        QueryWrapper<Menu> qw = new QueryWrapper<>();
        qw.eq("available", Constant.AVAILABLE_TRUE);
        qw.and(new Consumer<QueryWrapper<Menu>>() {
            @Override
            public void accept(QueryWrapper<Menu> menuQueryWrapper) {
                menuQueryWrapper.eq("type", Constant.TYPE_TOP_MENU).or().eq("type", Constant.TYPE_LEFT_MENU);
            }
        });
        qw.orderByAsc("ordernum");
        return this.getBaseMapper().selectList(qw);
    }


    @Override
    public DataGridlView queryAllMenu(Menu menu) {
        QueryWrapper<Menu> qw = new QueryWrapper<>();
        qw.orderByAsc("ordernum");
        qw.eq(!StringUtils.isEmpty(menu.getAvailable()), "available", menu.getAvailable());
        List<Menu> list = getBaseMapper().selectList(qw);
        return new DataGridlView((long) list.size(), list);
    }

    /**
     * 查询最大的排序码
     */
    @Override
    public Integer queryMenuMaxOrderNum() {
        return getBaseMapper().queryMenuMaxOrderNum();
    }

    /**
     * 添加菜单和权限
     */
    @Override
    public Menu addMenu(Menu menu) {
        // 如果不是按钮权限
        if (StringUtils.isEmpty(menu.getTypecode())) {
            // 根据pid拿到父级菜单的typecode
            Menu pmenu = getBaseMapper().selectById(menu.getPid());
            String typecode = pmenu.getTypecode();
            menu.setTypecode(typecode);
        }
        if (menu.getType().equals("topmenu")) {
            menu.setPid(0);
        }
        getBaseMapper().insert(menu);
        return menu;
    }

    /**
     * 修改菜单和权限
     */
    @Override
    public Menu updateMenu(Menu menu) {
        // 根据pid拿到父级菜单的typecode
        if (!menu.getTypecode().equals(Constant.TYPE_PERMISSION)){
            // 根据pid拿到父级菜单的typecode
            Menu pmenu = getBaseMapper().selectById(menu.getPid());
            String typecode = pmenu.getTypecode();
            menu.setTypecode(typecode);
        }
        getBaseMapper().updateById(menu);
        return menu;
    }

    @Override
    public Menu updateMenuBtn(Menu menu) {
        getBaseMapper().updateById(menu);
        return menu;
    }

    /**
     * 返回当前菜单的子节点的数量
     */
    @Override
    public DataGridlView getMenuChildrenCountById(Integer id) {
        Integer count = getBaseMapper().getMenuChildrenCountById(id);
        return new DataGridlView(count);
    }

    /**
     * 删除菜单和权限
     */
    @Override
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    /**
     * 根据用户id查询菜单数据的集合
     */
    @Override
    public List<Menu> queryMenuForListByUserId(Integer id) {
        // 根据用户id查询角色id集合
        List<Integer> roleIds = userMapper.queryRoleIdsByUid(id);
        System.out.println("roleIds:" + roleIds);
        if (roleIds != null && roleIds.size() > 0) {
            // 根据角色id查询菜单id集合
            List<Integer> menuIds = roleMapper.queryMenuIdsByRids(roleIds);
            System.out.println("menuIds:" + menuIds);
            if (menuIds != null && menuIds.size() > 0) {
                QueryWrapper<Menu> qw = new QueryWrapper<>();
                qw.eq("available", Constant.AVAILABLE_TRUE);
                qw.and(new Consumer<QueryWrapper<Menu>>() {
                    @Override
                    public void accept(QueryWrapper<Menu> menuQueryWrapper) {
                        menuQueryWrapper.eq("type",Constant.TYPE_TOP_MENU)
                                .or().eq("type",Constant.TYPE_LEFT_MENU);
                    }
                });
                qw.in("id", menuIds);
                qw.orderByAsc("ordernum");
                return getBaseMapper().selectList(qw);
            } else {
                return new ArrayList<>();
            }
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * 根据用户ID查询权限编码集合
     */
    @Override
    public List<String> queryPermissionCodeByUserId(Integer id) {
        List<Integer> roleIds = userMapper.queryRoleIdsByUid(id);

        if (roleIds != null && roleIds.size() > 0) {
            List<Integer> menuIds = roleMapper.queryMenuIdsByRids(roleIds);
            System.out.println(menuIds);
            if (menuIds != null && menuIds.size() > 0) {
                QueryWrapper<Menu> qw = new QueryWrapper<>();
                qw.eq("available",Constant.AVAILABLE_TRUE);
                qw.eq("type",Constant.TYPE_PERMISSION);
                qw.in("id",menuIds);
                qw.orderByAsc("ordernum");
                List<Menu> menus = getBaseMapper().selectList(qw);
                ArrayList<String> permissions = new ArrayList<>();
                for (Menu menu : menus) {
                    permissions.add(menu.getTypecode());
                }
                return permissions;
            } else {
                return null;
            }
        } else {
            return null;
        }
    }
}
